package com.ultrasoft.farmasoft.repository;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Transaction;

@Repository(value = "transactionDao")
@Transactional(readOnly = false)
public class TransactionDaoImpl implements TransactionDao {

	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}	
	
	public List<Transaction> getListTransactionByPharmacyIdByUserId(Integer pharmacyId,Integer userId) {
		TypedQuery<Transaction> q = em.createNamedQuery("Transaction.findByPharmacyIdByUserId",Transaction.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("userId",userId);
    	return q.getResultList();
	}
	
	public List<Transaction> getListTransactionByStartDateByEndDate(Date start,Date end) {
		TypedQuery<Transaction> q = em.createNamedQuery("Transaction.findByDateStartTransactionByEndTransaction",Transaction.class);
    	q.setParameter("start",start);
    	q.setParameter("end",end);    	
    	return q.getResultList();
	}
	public List<Transaction> getListTransactionByPharmacyIdByStartDateByEndDate(Integer pharmacyId,Date start,Date end) {
		TypedQuery<Transaction> q = em.createNamedQuery("Transaction.findByPharmacyIdByDateStartTransactionByEndTransaction",Transaction.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("start",start);
    	q.setParameter("end",end);    	
    	return q.getResultList();
	}
	
	
	public List<Transaction> getListTransaction() {
		TypedQuery<Transaction> q = em.createNamedQuery("Transaction.findAll",Transaction.class);
    	return q.getResultList();
	}
	
	public List<Transaction> getListTransactionByDateTransaction(Date dateTransaction) {
		TypedQuery<Transaction> q = em.createNamedQuery("Transaction.findByDateTransaction",Transaction.class);
    	q.setParameter("dateTransaction",dateTransaction);
    	return q.getResultList();
	}
	
	public List<Transaction> getListTransactionByPackageId(Integer packageId) {
		TypedQuery<Transaction> q = em.createNamedQuery("Transaction.findByPackageId",Transaction.class);
    	q.setParameter("packageId",packageId);
    	return q.getResultList();
	}

	public List<Transaction> getListTransactionByClientId(Integer clientId) {
		TypedQuery<Transaction> q = em.createNamedQuery("Transaction.findByClientId",Transaction.class);
    	q.setParameter("clientId",clientId);
    	return q.getResultList();
	}

	public List<Transaction> getListTransactionByOperationId(Integer operationId) {
		TypedQuery<Transaction> q = em.createNamedQuery("Transaction.findByClientId",Transaction.class);
    	q.setParameter("operationId",operationId);
    	return q.getResultList();
	}

	public List<Transaction> getListTransactionByPeriodIds(Integer periodIds) {
		TypedQuery<Transaction> q = em.createNamedQuery("Transaction.findByPeriodIds",Transaction.class);
    	q.setParameter("periodIds",periodIds);
    	return q.getResultList();
	}

	public List<Transaction> getListTransactionByTransactionId(	Integer transactionId) {
		TypedQuery<Transaction> q = em.createNamedQuery("Transaction.findByTransactionId",Transaction.class);
    	q.setParameter("transactionId",transactionId);
    	return q.getResultList();
	}

	public Transaction findTransactionByPeriodIdsByTransactionId(Integer periodIds, Integer transactionId) {
		Transaction res = null;
    	Query  q = em.createNamedQuery("Transaction.findByPeriodIdsByTransactionId");
    	q.setParameter("periodIds",periodIds);
    	q.setParameter("transactionId",transactionId);
    	if(q.getResultList().size()==1)
    		res=(Transaction)q.getSingleResult();
    	return res;
	}

	public Integer findTransactionIdByPeriodIdsMax(Integer periodIds){
		Integer res=0;
		if(!getListTransactionByPeriodIds(periodIds).isEmpty()){
			Query  q = em.createNamedQuery("Transaction.findTransactionIdByPeriodIdsMax");
	    	q.setParameter("periodIds",periodIds);
	    	if(q.getResultList().size()==1)
	    		res=(Integer)q.getSingleResult();
		}
		return res;
	}
	
	public void remove(Transaction transaction) {
		em.remove(em.getReference(Transaction.class,findTransactionByPeriodIdsByTransactionId(transaction.getTransactionPK().getPeriodIds(), transaction.getTransactionPK().getTransactionId())));
	}

	public void update(Transaction transaction) {
		em.merge(transaction);
	}

	public void add(Transaction transaction) {
		em.persist(transaction);
	}
}
